<?
$id = isset($_GET['id']) ? $_GET['id'] : die("Error 404: Member not found");

if(!preg_match("/^[0-9]+$/", $id)) {
	die("Error: Invalid id");
}

include("top.php");

//------------- VÄRVADE MEDLEMMAR ---------------//
$sSql = "SELECT * FROM tblMedlemmar WHERE varvadAvId = '" . $id . "' ORDER BY username";
$result = mysql_query($sSql) or die(mysql_error());

$sVarvade = "";

while($row = mysql_fetch_array($result)) {
	$sVarvade .= "<a href='/medlem" . $row['id'] . "/" . sToUrl($row['username']) . ".php'>" . $row['username'] . "</a>, \n";
}

$sVarvade = substr($sVarvade, 0, -3);

//--------------- MEDLEMSINFO ----------------------//
$sSql = "SELECT tblMedlemmar.*, tblRegioner.regionNamn FROM tblMedlemmar 
		LEFT OUTER JOIN tblRegioner ON tblMedlemmar.regionsId = tblRegioner.id
		WHERE tblMedlemmar.id = '" . $id . "'";
$result = mysql_query($sSql) or die(mysql_error());

$row = mysql_fetch_array($result) or die("Error: Member not found");

$imgName = strlen($row['avatar']) > 0 ? $row['avatar'] : "ingenbild.gif";
?>

<table cellpadding=0 cellspacing=0 border=0>
  <tr>
  	<td colspan=3 class='memberTitle'><?= $row['username'] ?></td>
  </tr>
  <tr>
  	<td width='290' valign='top'>
    
    <table cellpadding=0 cellspacing=0 border=0>
      <tr>
      	<td width=120><b>Namn</b></td>
        <td width=170><?= $row['name'] ?></td>
      </tr>
      <tr>
      	<td width=120><b>Födelseår</b></td>
        <td width=170><?= $row['year'] > 0 ? $row['year'] : "-"; ?></td>
      </tr>
      <tr>
      	<td width=120><b>Epostadress</b></td>
        <td width=170><?= $row['email'] ?></td>
      </tr>
      <tr>
      	<td width=120><b>Bor i</b></td>
        <td width=170><?= $row['regionNamn'] ?><? if(strlen($row['ort']) > 0) echo(", " . $row['ort']); ?></td>
      </tr>
      <tr>
      	<td width=120><b>Pysslar helst med</b></td>
        <td width=170>Bollar</td>
      </tr>
      <tr>
      	<td width=120><b>ICQ nummer</b></td>
        <td width=170><?= $row['icq'] > 0 ? $row['icq'] : "-"; ?></td>
      </tr>
      <tr>
      	<td width=120><b>Statuspoäng</b></td>
        <td width=170><?= $row['status'] ?></td>
      </tr>
      <tr>
      	<td width=120><b>Värvad av</b></td>
        <td width=170></td>
      </tr>
      <tr>
      	<td width=120><b>Senast inloggad</b></td>
        <td width=170><?= date("Y-m-d H:i", strtotime($row['inloggad'])) ?></td>
      </tr>
      <tr>
      	<td width=120><b>Antal inloggningar</b></td>
        <td width=170><?= $row['inloggningar'] ?></td>
      </tr>
    </table>
    
    </td>
    <td width='10'><br></td>
    <td width='150' valign='top'><img src='/bilder/medlemmar/150px/<?= $imgName ?>' border=0></td>
  </tr>
  <? if(strlen($row['beskrivning'])) { ?>
  <tr>
  	<td colspan=3 style='padding-top: 20px;'>
    	<b>Personlig beskrivning</b><br>
        <?= nl2br($row['beskrivning']); ?>
    </td>
  </tr>
  <? }
  
  //------------- REKORD ---------------//
  $sSql = "SELECT * FROM tblRekord WHERE medlemsId = '" . $id . "' ORDER BY propId, antalProps DESC";
  $result = mysql_query($sSql) or die(mysql_error());

  if(mysql_num_rows($result) > 0) { ?>
  <tr>
  	<td colspan=3 style='padding-top: 20px;'>
    	<b>Personliga rekord</b><br>
        
        <table cellpadding=0 cellspacing=0 border=0>
        <?
		$arrProps = array(1 => "Bollar", 2 => "Käglor", 3 => "Ringar", 4 => "Studsbollar");
		
		while($row = mysql_fetch_array($result)) { ?>
          <tr>
            <td width=100><?= $row['antalProps'] . " " . $arrProps[$row['propId']] ?></td>
            <td width=100><?= $row['fangster'] ?> fångster</td>
            <td width=250><?= $row['kommentar'] ?></td>
          </tr>
        <? } ?>
        </table>
    
    </td>
  </tr>
  <? }

  if(strlen($sVarvade) > 0) { ?>
  <tr>
  	<td colspan=3 style='padding-top: 20px;'>
    	<b>Värvade medlemmar</b><br>
        <?= $sVarvade ?>    
    </td>
  </tr>
  <? } ?>
</table>

<? include("bottom.php"); ?>